<?php
/**
 * This is a quick-and-dirty draft QPanel object to do Create, Edit, and Delete functionality
 * of the SchoolUsedTitles class.  It uses the code-generated
 * SchoolUsedTitlesMetaControl class, which has meta-methods to help with
 * easily creating/defining controls to modify the fields of a SchoolUsedTitles columns.
 *
 * Any display customizations and presentation-tier logic can be implemented
 * here by overriding existing or implementing new methods, properties and variables.
 *
 * NOTE: This file is overwritten on any code regenerations.  If you want to make
 * permanent changes, it is STRONGLY RECOMMENDED to move both school_used_titles_edit.php AND
 * school_used_titles_edit.tpl.php out of this Form Drafts directory.
 *
 * @package My QCubed Application
 * @subpackage Drafts
 */
class SchoolUsedTitlesEditPanel extends QPanel {
    // Local instance of the SchoolUsedTitlesMetaControl
    protected $mctSchoolUsedTitles;

    // Controls for SchoolUsedTitles's Data Fields

    public $lstStage;
    public $lstSeries;
    public $lstUsedSince;
    public $txtNote;

    public $lblMsg;


    // Other ListBoxes (if applicable) via Unique ReverseReferences and ManyToMany References

    // Other Controls
    public $btnSave;
    public $btnDelete;
    public $btnCancel;

    // Callback
    protected $strClosePanelMethod;
    public static function AutoListSeries_Change( $strParameter ) {
        try {
            $objMemberArray =  Series::QueryArray(
                    QQ::Like(QQN::Series()->Name, $strParameter . '%'),
                    QQ::Clause(QQ::OrderBy(QQN::Series()->Name))
            );
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            throw $objExc;
        }
        $result = array();

        foreach($objMemberArray as $objMember) {
            $result[] = $objMember->Name . "|" . $objMember->Id;
        }

        return $result;

    }
    public function __construct($objParentObject, $strClosePanelMethod, $intId = null, $strControlId = null) {
        // Call the Parent
        try {
            parent::__construct($objParentObject, $strControlId);
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            throw $objExc;
        }

        // Setup Callback and Template
        $this->strTemplate = __DOCROOT__ . __MARKETING__ . '/tpl/SchoolUsedTitlesEditPanel.tpl.php';

        $this->strClosePanelMethod = $strClosePanelMethod;

        // Construct the SchoolUsedTitlesMetaControl
        // MAKE SURE we specify "$this" as the MetaControl's (and thus all subsequent controls') parent
        $this->mctSchoolUsedTitles = SchoolUsedTitlesMetaControl::Create($this, $intId);

        // Call MetaControl's methods to create qcontrols based on SchoolUsedTitles's data fields

        $this->lstStage = $this->mctSchoolUsedTitles->lstStage_Create();
        $this->lstSeries = $this->mctSchoolUsedTitles->lstSeries_Create();
        $this->lstUsedSince = $this->mctSchoolUsedTitles->lstUsedSince_Create();
        $this->txtNote = $this->mctSchoolUsedTitles->txtNote_Create();

        $this->lblMsg = new QLabel($this);
        
        // Create Buttons and Actions on this Form
        $this->btnSave = new QButton($this);
        $this->btnSave->Text = QApplication::Translate('Save');
        $this->btnSave->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnSave_Click'));
        $this->btnSave->CausesValidation = $this;

        $this->btnCancel = new QButton($this);
        $this->btnCancel->Text = QApplication::Translate('Cancel');
        $this->btnCancel->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnCancel_Click'));

        $this->btnDelete = new QButton($this);
        $this->btnDelete->Text = QApplication::Translate('Delete');
        $this->btnDelete->AddAction(new QClickEvent(), new QConfirmAction(QApplication::Translate('Are you SURE you want to DELETE this') . ' ' . QApplication::Translate('SchoolUsedTitles') . '?'));
        $this->btnDelete->AddAction(new QClickEvent(), new QAjaxControlAction($this, 'btnDelete_Click'));
        $this->btnDelete->Visible = $this->mctSchoolUsedTitles->EditMode;
    }
    public function Validate(){
        if ( ($this->txtNote->Text == "")&& ($this->lstSeries->Text == "") ){
            $this->lblMsg->Text="<font color='red'>All fields are empty.</font>";
            return false;
        }
        $this->lblMsg->Text="";
        return true;
    }
    // Control AjaxAction Event Handlers
    public function btnSave_Click($strFormId, $strControlId, $strParameter) {
        if ($this->Validate()){
            $this->mctSchoolUsedTitles->SaveSchoolUsedTitles();
            $this->CloseSelf(true);
        }
    }

    public function btnDelete_Click($strFormId, $strControlId, $strParameter) {
        // Delegate "Delete" processing to the SchoolUsedTitlesMetaControl
        $this->mctSchoolUsedTitles->DeleteSchoolUsedTitles();
        $this->CloseSelf(true);
    }

    public function btnCancel_Click($strFormId, $strControlId, $strParameter) {
        $this->CloseSelf(false);
    }

    // Close Myself and Call ClosePanelMethod Callback
    protected function CloseSelf($blnChangesMade) {
        $this->lstUsedSince->SelectedValue = 0;
        $this->lstStage->SelectedValue = 0;
        $this->txtNote->Text="";
        $this->lstSeries->Text = "";
        $this->objParentControl->Refresh();
        
        //$strMethod = $this->strClosePanelMethod;
        $this->objParentControl->CloseReOpenEditPanel();
    }


}
?>